package app.esb.common;

import cn.lily.com.lang.Basic;

/**
 * 数字处理工具类
 * @author JiaJ
 * @date 2017年12月2日
 *
 */
public class NumberTools {
	
	/**
	 * 保存位数
	 * @param active
	 * @param scale
	 * @return
	 */
	public static String scale(double active,int scale){
		return String.format("%."+scale+"f",active);
	}
	
	/**
	 * KB信息转换
	 * @param info
	 * @return
	 */
	public static String toSizeString(double info){
		
		if(info < 1000.0){
			double active = info;
			
			return scale(active,2) + "KB";
		}
		
		else if(info >= 1000 && info < 1000000 ){
			
			double active = info/1024;
			
			return scale(active,2) + "MB";
		}
		
		else if(info >= 1000000 && info < 1000000000 ){
			
			double active = info/1024/1024;
			return scale(active,2) + "GB";
		}
		
		else{
			
			double active = info/1024/1024/1024;
			
			return scale(active,2) + "TB";
		}
		
	}
	
	/**
	 * 转换为KB
	 * @param info
	 * @param scale 位数
	 * @return
	 */
	public static String toKBSize(String info,int scale){
		return scale(toKBSize(info),scale);
	}
	
	/**
	 * 转换为KB大小
	 * @param info
	 * @return
	 */
	public static double toKBSize(String info){
		
		if(info == null || "".equals(info)){
			info = "0.00KB";
		}
		
		if(info.endsWith("TB")){
			info = info.substring(0,info.length()-2);
			double infoTB = Basic.getDouble(info);
			return infoTB * 1024 * 1024 * 1024;
		}
		
		else if(info.endsWith("T")){
			info = info.substring(0,info.length()-1);
			double infoTB = Basic.getDouble(info);
			return infoTB * 1024 * 1024 * 1024;
		}
		
		else if(info.endsWith("G")){
			info = info.substring(0,info.length()-1);
			double infoGB = Basic.getDouble(info);
			return infoGB * 1024 * 1024;
		}
		
		else if(info.endsWith("GB")){
			info = info.substring(0,info.length()-2);
			double infoGB = Basic.getDouble(info);
			return infoGB * 1024 * 1024;
		}
		
		else if(info.endsWith("M")){
			info = info.substring(0,info.length()-1);
			double infoMB = Basic.getDouble(info);
			return infoMB * 1024;
		}
		
		else if(info.endsWith("MB")){
			info = info.substring(0,info.length()-2);
			double infoMB = Basic.getDouble(info);
			return infoMB * 1024;
		}
		
		else if(info.endsWith("K")){
			info = info.substring(0,info.length()-1);
			double infoKB = Basic.getDouble(info);
			return infoKB;
		}
		
		else if(info.endsWith("KB")){
			info = info.substring(0,info.length()-2);
			double infoKB = Basic.getDouble(info);
			return infoKB;
		}
		
		return Basic.getDouble(info);
		
	}
	
}
